package com.gitee.neuray.security.admin.element.biz;

import com.alibaba.fastjson.JSON;
import com.gitee.neuray.security.admin.vo.AdminElement;
import com.gitee.neuray.security.common.util.EntityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import org.springframework.transaction.annotation.Transactional;
import com.gitee.neuray.security.common.biz.BaseBiz;

import com.gitee.neuray.security.admin.element.mapper.AdminElementMapper;
import com.gitee.neuray.security.admin.element.entity.AdminElementEntity;
import tk.mybatis.mapper.entity.Example;


@Service
@Transactional(rollbackFor = Exception.class)
public class AdminElementBiz extends BaseBiz<AdminElementMapper,AdminElementEntity> {
    @Override
    protected String getPageName() {
        return "AdminElement"+"Biz";
    }


    /**
     * 通过menuid获取menu下的所有的菜单
     * @param menuId menuId
     * @return Element信息集
     */
    public List<AdminElement> listMenuElement(String menuId){
        Example params = new Example(AdminElementEntity.class);
        params.createCriteria().andEqualTo("menuId",menuId);
        List<AdminElementEntity> elements = mapper.selectByExample(params);
        return JSON.parseArray(JSON.toJSONString(elements),AdminElement.class);
    }
    /**
     * 保存或修改menu下的element
     * @param menuId 菜单id
     * @param elementList 绑定的element
     */
    public void modifyMenuElement(String menuId,List<AdminElement> elementList){
        AdminElementEntity elementDO = new AdminElementEntity();
        elementDO.setMenuId(menuId);
        mapper.delete(elementDO);
        if(elementList.size()>0){
            elementList.stream().forEach(elementVO->{
                BeanUtils.copyProperties(elementVO,elementDO);
                EntityUtils.setCreatAndUpdatInfo(elementDO);
                mapper.insertSelective(elementDO);
            });
        }
    }
}
